package com.suncnpap.library.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suncnpap.library.model.*;
import com.suncnpap.statistics.model.LatelyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.app.util.beanutil.BeanUtil;
import com.app.util.common.UuidUtil;
import com.app.util.database.mybatis.Page;
import com.suncnpap.library.service.LibraryService;
import com.suncnpap.base.common.GlobaUser;
import com.suncnpap.library.mapper.BookshelfMapper;
import com.suncnpap.library.mapper.ChineseClassificationMapper;
import com.suncnpap.library.mapper.CollectionLocationMapper;
import com.suncnpap.library.mapper.LibraryMapper;
import com.suncnpap.library.mapper.LotNumberMapper;

/** 
 * 说明： 书库Service实现类
 * @version
 */
@Service("libraryService")
public class LibraryServiceImpl implements LibraryService{

	
	@Autowired
	LibraryMapper libraryMapper;
	
	@Autowired
	LotNumberMapper lotNumberMapper;
	
	@Autowired
	BookshelfMapper bookshelfMapper;
	
	@Autowired
	CollectionLocationMapper collectionLocationMapper;
	
	@Autowired
	ChineseClassificationMapper chineseClassificationMapper;
	
	@Override
	public void add(Library obj) {
		// TODO Auto-generated method stub
		libraryMapper.add(obj);
	}

	@Override
	public void update(Library obj) {
		// TODO Auto-generated method stub
		libraryMapper.edit(obj);
	}

	@Override
	public void deleteById(String strId) {
		// TODO Auto-generated method stub
		libraryMapper.deleteById(strId);
		
	}
	@Override
	public void deleteMore(String[] strIds) {
		// TODO Auto-generated method stub
		libraryMapper.deleteMore(strIds);
	}

	@Override
	public Page list(Page page) {
		// TODO Auto-generated method stub
		List<Library> list = libraryMapper.datalistPage(page);
		page.setDataList(list);
		return page;
	}


	@Override
	public int findCount(Page page) {
		// TODO Auto-generated method stub
		return libraryMapper.findCount(page);
	}

	@Override
	public Library findById(String strId) {
		// TODO Auto-generated method stub
		return libraryMapper.findById(strId);
	}
	
	@Override
	public void updateBorrowById(String strBookBarcode, String strSchoolId){
		libraryMapper.updateBorrowById(strBookBarcode, strSchoolId);
	}
	
	@Override
	public void updateReturnById(String strBookBarcode, String strSchoolId){
		libraryMapper.updateReturnById(strBookBarcode, strSchoolId);
	}
	
	@Override
	public Page libraryList(Page page) {
		// TODO Auto-generated method stub
		List<Library> list = libraryMapper.librarylistPage(page);
		page.setDataList(list);
		return page;
	}
	
	@Override
	public Library findByBookBarcode(String strBookBarcode, String strSchoolId) {
		// TODO Auto-generated method stub
		return libraryMapper.findByBookBarcode(strBookBarcode, strSchoolId);
	}
	
	@Override
	public Page categoryList(Page page) {
		// TODO Auto-generated method stub
		List<Library> list = libraryMapper.categorylist(page);
		page.setDataList(list);
		return page;
	}
	
	@Override
	public Page collectionTypelist(Page page) {
		// TODO Auto-generated method stub
		List<Library> list = libraryMapper.collectionTypelist(page);
		page.setDataList(list);
		return page;
	}
	
	
	@Override
	public  void addList(List<Library> objList){
		if(objList!=null && objList.size()>0){
			for(Library library:objList){
				String strTitle = library.getStrPositiveTitle();
				if(library!=null && !strTitle.equals("")){
					String strSchoolId = library.getStrSchoolId();
					String strBookBarcode = library.getStrBookBarcode();
					Library obj = libraryMapper.findByBookBarcode(strBookBarcode, strSchoolId);
					if(obj==null){
						
						String strLotNumber = library.getStrLotNumber();
						LotNumber lotNumber = lotNumberMapper.findByLotNumber(strLotNumber, strSchoolId);
						if(lotNumber==null){
							lotNumber = new LotNumber();
							BeanUtil.copyBean(library, lotNumber);
							lotNumber.setStrLotNumber(strLotNumber);
							lotNumber.setStrId(UuidUtil.get32UUID());
							lotNumberMapper.add(lotNumber);
						}
						
						String clName = library.getStrCollectionLocation();
						CollectionLocation collectionLocation = collectionLocationMapper.findByName(clName, strSchoolId);
						if(collectionLocation==null){
							collectionLocation = new CollectionLocation();
							BeanUtil.copyBean(library, collectionLocation);
							collectionLocation.setStrName(clName);
							collectionLocation.setStrId(UuidUtil.get32UUID());
							collectionLocationMapper.add(collectionLocation);
						}
						library.setStrCollectionLocation(clName);
						library.setStrCollectionLocationId(collectionLocation.getStrId());
						
						String strBookshelf = library.getStrBookshelf();
						Bookshelf bookshelf = bookshelfMapper.findByName(strBookshelf, strSchoolId);
						if(bookshelf==null){
							bookshelf = new Bookshelf();
							BeanUtil.copyBean(library, bookshelf);
							bookshelf.setStrCollectionLocation(library.getStrCollectionLocation());
							bookshelf.setStrCollectionLocationId(library.getStrCollectionLocationId());
							bookshelf.setStrBookshelfName(strBookshelf);
							bookshelf.setStrId(UuidUtil.get32UUID());
							bookshelfMapper.add(bookshelf);
						}
						library.setStrBookshelf(bookshelf.getStrId());
						
						String strClassNumber = library.getStrClassNumber();
						library.setStrCallNumber(strClassNumber+" "+library.getStrBookNumber());
						
						ChineseClassification chineseClassification = chineseClassificationMapper.findByCode(strClassNumber, strSchoolId);
						if(chineseClassification!=null){
							library.setStrCategoryName(chineseClassification.getStrNodeName());
						}
						
						library.setStrCurrencyUnit("人民币");
						library.setIntNumberLoans(0);					//借出次数  
						library.setIntIsLogoff(0);				//是否注销  
						library.setIntExistingStock(1);
						library.setIntTotaIinventory(1);
						libraryMapper.add(library);
					}
					
				}
			}
		}
		
	}
	
	@Override
	public String getMaxBookNumber(String strClassNumber, String strSchoolId){
		return libraryMapper.getMaxBookNumber(strClassNumber, strSchoolId);
	}

	/**
	 * 热门图书
	 * @param hotVoConditions
	 * @param page
	 * @param size
	 * @return
	 */
	@Override
	public PageInfo<hotResult> getHotBookList(hotVo hotVoConditions, Integer page, Integer size) {
		PageHelper.startPage(page,size);
		List<hotResult> libraries=libraryMapper.getHotBooks(hotVoConditions);
		PageInfo<hotResult> hotBooksList = new PageInfo<>(libraries);
		return hotBooksList;
	}

	@Override
	public List<Library> pressList() {
		List<Library> press=libraryMapper.pressList();
		return press;
	}

	@Override
	public Library strBookBarcode(String strBookBarcode, String strSchoolId) {
		Library library=libraryMapper.strBookBarcode(strBookBarcode,strSchoolId);
		return library;
	}

	@Override
	public List<Library> manyLately(LatelyVo latelyVo) {
		List<Library> libraryMany=libraryMapper.manyLately(latelyVo);
		return libraryMany;
	}
}

